<?php
/**
 * Created by PhpStorm.
 * User: Leslie
 * Date: 2016/6/16
 * Time: 9:47
 */

namespace app\models;

use app\librarys\DbCacheUtility;
use app\librarys\Inteface;

class Institution
{

    /**
     * 机构列表
     * @param int $institutionId
     * @return array
     */
    public static function getInstitutionSelect($institutionId = 0)
    {
        $filterSQL = '1=1 ';

        if ($institutionId != '0') {
            $filterSQL = $filterSQL . ' and parent_code= :institutionId';
        }
        $sql = "SELECT code,name FROM t_institution WHERE $filterSQL order by name ";
        $cmd = \Yii::$app->db->createCommand($sql);
        if ($institutionId != '0') {
            $cmd->bindValue(':institutionId', $institutionId);
        }
        $result = $cmd->queryAll();

        return $result;
    }

    /**
     * 获取所有启用的机构列表(不分页)
     * @return array
     */
    public static function getAllEnabledList()
    {
        $cmd = \Yii::$app->db->createCommand('SELECT * FROM t_institution WHERE is_enabled=1');
        $items = $cmd->queryAll();
        return $items;
    }

    /**
     * 获取所有启用的机构列表,并按父子结构排列,通过每个item中的level来确定是等级,0为顶级
     * @param $institutionCode
     * @return array
     */
    public static function getAllEnabledListWithLevel($institutionCode)
    {
        if ($institutionCode == '0') {
            $items = self::_getChildrenList($institutionCode, 0);
        } else {
            $cmd = \Yii::$app->db->createCommand('SELECT * FROM t_institution WHERE is_enabled=1 AND code=:institutionCode');
            $cmd->bindValue(':institutionCode', $institutionCode);
            $result = DbCacheUtility::queryOne($cmd, 600);
            $result['level'] = 0;

            $items = [];
            array_push($items, $result);

            $children = self::_getChildrenList($institutionCode, 1);
            if (count($children) > 0) {
                foreach ($children as $child) {
                    array_push($items, $child);
                }
            }
        }
        return $items;
    }

    /**
     * @param $parentCode
     * @param $level
     * @return array
     */
    private static function _getChildrenList($parentCode, $level)
    {
        $cmd = \Yii::$app->db->createCommand('SELECT * FROM t_institution WHERE is_enabled=1 AND parent_code=:parentCode');
        $cmd->bindValue(':parentCode', $parentCode);
        $resultList = DbCacheUtility::queryAll($cmd, 600);

        $items = [];

        if (count($resultList) > 0) {
            for ($i = 0; $i < sizeof($resultList); $i++) {
                $result = $resultList[$i];
                $result['level'] = $level;
                if ($i + 1 == sizeof($resultList)) {
                    $result['is_last'] = true;
                } else {
                    $result['is_last'] = false;
                }
                array_push($items, $result);

                $children = self::_getChildrenList($result['code'], $level + 1);
                if (count($children) > 0) {
                    foreach ($children as $child) {
                        array_push($items, $child);
                    }
                }
            }
        }
        return $items;
    }

    /**
     * 获取所有启用的机构列表,并按父子层级关系排列
     * @param $institutionCode
     * @return array
     */
    public static function getAllEnabledListWithHierarchy($institutionCode)
    {
        if ($institutionCode == '0') {
            $items = self::_getChildrenListWithHierarchy($institutionCode);
        } else {
            $cmd = \Yii::$app->db->createCommand('SELECT * FROM t_institution WHERE is_enabled=1 AND code=:institutionCode');
            $cmd->bindValue(':institutionCode', $institutionCode);
            $items = DbCacheUtility::queryOne($cmd);
            if ($items != false) {
                $children = self::_getChildrenListWithHierarchy($items['code']);
                if (count($children)) {
                    $result['children'] = $children;
                }
            }
        }
        return $items;
    }

    private static function _getChildrenListWithHierarchy($parentCode)
    {
        $cmd = \Yii::$app->db->createCommand('SELECT * FROM t_institution WHERE is_enabled=1 AND parent_code=:parentCode');
        $cmd->bindValue(':parentCode', $parentCode);
        $items = DbCacheUtility::queryAll($cmd);

        foreach ($items as &$item) {
            $children = self::_getChildrenListWithHierarchy($item['code']);
            if (count($children) > 0) {
                $item['children'] = $children;
            }
        }

        return $items;
    }

    /**
     * 获取当前机构下的所有直属机构
     * @param string $institutionCode
     * @return mixed
     */
    public static function getAllEnabledChildren($institutionCode)
    {
        $cmd = \Yii::$app->db->createCommand('SELECT * FROM t_institution WHERE is_enabled=1 AND parent_code=:parentCode');
        $cmd->bindValue(':parentCode', $institutionCode);
        $items = DbCacheUtility::queryAll($cmd);
        return $items;
    }

    /**
     * 获取当前机构下的所有直属机构(包括当前机构)
     * @param string $institutionCode
     * @return mixed
     */
    public static function getAllEnabledSelfChildren($institutionCode)
    {
        $cmd = \Yii::$app->db->createCommand('SELECT * FROM t_institution WHERE is_enabled=1 AND code=:parentCode');
        $cmd->bindValue(':parentCode', $institutionCode);
        $items = DbCacheUtility::queryOne($cmd);
        if ($items != false) {
            $cmd = \Yii::$app->db->createCommand('SELECT * FROM t_institution WHERE is_enabled=1 AND parent_code=:parentCode');
            $cmd->bindValue(':parentCode', $institutionCode);
            $children = DbCacheUtility::queryAll($cmd);

            if (count($children)) {
                $items['children'] = $children;
            }
        }
        return $items;
    }


    /**
     * 机构信息
     * @param $institutionId
     * @return array|false
     */
    public static function getInstitution($institutionId)
    {
        $sql = "SELECT * FROM v_institution_site WHERE code= :institutionId";
        $cmd = \Yii::$app->db->createCommand($sql);
        $cmd->bindValue(':institutionId', $institutionId);
        $result = $cmd->queryOne();

        return $result;
    }

    /**
     * 新增机构
     * @param $record (code,parentCode,name,address,isEnabled,isAutoOrder,isCashOrder,isAllowImportOrder,billModel,integralRate,integralUnit,remark,siteName,domain,themePc,themeM,account,token)
     * @return false|int|null|string
     */
    public static function addInstitution($record, &$message = '')
    {
        $result = 0;
        $code = null;
        if ($record['code'] == '0' || $record['code'] == '') {
            //通过接口获取机构编码
            $response = Inteface::setInstitutionCode($record);
            if ($response->status == 0) {
                $code = $response->data;
            } else {
                $code = '';
                $message = $response->message;
            }
            //$result = $return->status; //20160905 删除 原因：由于接口转到了公用统一接口，统一接口返回的结构有变
            //$code = $return->data; //20160905 删除 原因：由于接口转到了公用统一接口，统一接口返回的结构有变
            if ($code == '') {
                $result = 1;
            }
        } else {
            $code = $record['code'];
        }

        if ($result == 0) { //20160905 删除 原因：由于接口转到了公用统一接口，统一接口返回的结构有变
            //     $result = 1; //20160905 删除 原因：由于接口转到了公用统一接口，统一接口返回的结构有变
            // } else { //20160905 删除 原因：由于接口转到了公用统一接口，统一接口返回的结构有变

            $sql = "call p_save_institution(:in_code,:in_parentCode,:in_name,:in_address,:in_isEnabled,:in_isAutoOrder,:in_isCashOrder,:in_isAllowImportOrder,:in_isCustomPrice
        ,:in_billModel,:in_integralRate,:in_integralUnit,:in_remark,:in_siteName,:in_domain,:in_themePc,:in_themeM,:in_salesmanName,:in_salesmanPhone)";

            $cmd = \Yii::$app->db->createCommand($sql);
            $cmd->bindValue(':in_code', $code);
            $cmd->bindValue(':in_parentCode', $record['parentCode']);
            $cmd->bindValue(':in_name', $record['name']);
            $cmd->bindValue(':in_address', $record['address']);
            $cmd->bindValue(':in_isEnabled', $record['isEnabled']);
            $cmd->bindValue(':in_isAutoOrder', $record['isAutoOrder']);
            $cmd->bindValue(':in_isCashOrder', $record['isCashOrder']);
            $cmd->bindValue(':in_isAllowImportOrder', $record['isAllowImportOrder']);
            if (isset($record['isCustomPrice'])) {
                $cmd->bindValue(':in_isCustomPrice', $record['isCustomPrice']);
            } else {
                $cmd->bindValue(':in_isCustomPrice', 0);
            }
            $cmd->bindValue(':in_billModel', $record['billModel']);
            $cmd->bindValue(':in_integralRate', $record['integralRate']);
            $cmd->bindValue(':in_integralUnit', $record['integralUnit']);
            $cmd->bindValue(':in_remark', $record['remark']);
            $cmd->bindValue(':in_siteName', $record['siteName']);
            $cmd->bindValue(':in_domain', $record['domain']);
            $cmd->bindValue(':in_themePc', $record['themePc']);
            $cmd->bindValue(':in_themeM', $record['themeM']);
            //$cmd->bindValue(':in_account', $record['account']);
            //$cmd->bindValue(':in_token', $record['token']);
            $cmd->bindValue(':in_salesmanName', $record['salesmanName']);
            $cmd->bindValue(':in_salesmanPhone', $record['salesmanPhone']);

            $result = $cmd->queryScalar();
        }//20160905 删除 原因：由于接口转到了公用统一接口，统一接口返回的结构有变
        if ($result == 0) {
            return $code;
        } else {
            return null;
        }
    }

    /**
     * 机构列表
     * @param $start
     * @param $pagesize
     * @param $status
     * @param $filter  机构名称或编码
     * @param $filterP 父机构名称或编码
     * @param $institutionId
     * @return array
     */
    public static function getInstitutionList($start, $pagesize, $status, $filter, $filterP, $institutionId)
    {
        $filterSQL = '1=1 ';

        if ($institutionId != '0') {

            $filterSQL = $filterSQL . ' and parentCode= :institutionId';

        }

        if ($filter != '0') {
            $filterSQL = $filterSQL . ' and ((name like :filter) or (code like :filter))';
        }

        if ($filterP != '0') {

            $filterSQL = $filterSQL . ' and  parentCode = :filterP';

        }

        if ($status != '99') {

            $filterSQL = $filterSQL . ' and (isEnabled= :status)';

        }

        if ($pagesize == '0') {
            $sql = "SELECT i.*,c.contacter FROM v_institution_list i LEFT JOIN v_institution_contacter c ON i.code=c.institution_code  WHERE $filterSQL";
        } else {
            $sql = "SELECT * FROM v_institution_list WHERE $filterSQL ORDER BY code LIMIT $start,$pagesize";
        }


        $cmd = \Yii::$app->db->createCommand($sql);
        if ($institutionId != '0') {
            $cmd->bindValue(':institutionId', $institutionId);
        }
        if ($filter != '0') {
            $filter = '%' . $filter . '%';
            $cmd->bindValue(':filter', $filter);
        }
        if ($filterP != '0') {
            //$filterP = '%' . $filterP . '%';
            $cmd->bindValue(':filterP', $filterP);
        }
        if ($status != '99') {
            $cmd->bindValue(':status', (int)$status);
        }

        $result = $cmd->queryAll();

        return $result;
    }

    /**
     * 机构分页总数
     * @param $status
     * @param $filter
     * @param $filterP
     * @param $institutionId
     * @return mixed
     */
    public static function getInstitutionListTotal($status, $filter, $filterP, $institutionId)
    {
        $filterSQL = '1=1 ';

        if ($institutionId != '0') {
            $filterSQL = $filterSQL . ' and parentCode= :institutionId';
        }

        if ($filter != '0') {
            $filterSQL = $filterSQL . ' and ((name like :filter) or (code like :filter))';
        }

        if ($filterP != '0') {
            //$filterSQL = $filterSQL . ' and ((parentName like :filterP) or (parentCode like :filterP))';
            $filterSQL = $filterSQL . ' and parentCode= :filterP';
        }

        if ($status != '99') {
            $filterSQL = $filterSQL . ' and (isEnabled= :status)';
        }

        $sql = "SELECT count(*) AS totalnum FROM v_institution_list WHERE $filterSQL";

        $cmd = \Yii::$app->db->createCommand($sql);
        if ($institutionId != '0') {
            $cmd->bindValue(':institutionId', $institutionId);
        }
        if ($filter != '0') {
            $filter = '%' . $filter . '%';
            $cmd->bindValue(':filter', $filter);
        }
        if ($filterP != '0') {
            //$filterP = '%' . $filterP . '%';
            $cmd->bindValue(':filterP', $filterP);
        }
        if ($status != '99') {
            $cmd->bindValue(':status', (int)$status);
        }

        $result = $cmd->queryOne();

        return $result['totalnum'];


    }

    /**
     * 变更机构启用状态
     * @param $institutionId
     * @return false|null|string
     */
    public static function setInstitutionStatus($institutionId)
    {
        $sql = "call p_set_institution_status(:institutionId)";
        $cmd = \Yii::$app->db->createCommand($sql);
        $cmd->bindValue(":institutionId", $institutionId);
        $result = $cmd->queryScalar();

        return $result;

    }

    /**
     * 机构联系人分页总数
     * @param $filter
     * @param $institutionId
     * @return mixed
     */
    public static function getInstitutionContacterListTotal($filter, $institutionId)
    {

        $filterSQL = 'institution_code= :institutionId and isdelete=0';


        if ($filter != '0') {
            $filterSQL = $filterSQL . ' and ((name like :filter) or (phone like :filter) or (email like :filter))';
        }


        $sql = "SELECT count(*) AS totalnum FROM t_institution_contacter WHERE $filterSQL";

        $cmd = \Yii::$app->db->createCommand($sql);

        $cmd->bindValue(':institutionId', $institutionId);

        if ($filter != '0') {
            $filter = '%' . $filter . '%';
            $cmd->bindValue(':filter', $filter);
        }

        $result = $cmd->queryOne();

        return $result['totalnum'];
    }

    /**
     * 机构联系人列表
     * @param $start
     * @param $pagesize
     * @param $filter
     * @param $institutionId
     * @return array
     */
    public static function getInstitutionContacterList($start, $pagesize, $filter, $institutionId)
    {

        $filterSQL = 'institution_code= :institutionId and isdelete=0';


        if ($filter != '0') {
            $filterSQL = $filterSQL . ' and ((name like :filter) or (phone like :filter) or (email like :filter))';
        }


        $sql = "SELECT * FROM t_institution_contacter WHERE $filterSQL LIMIT $start,$pagesize";

        $cmd = \Yii::$app->db->createCommand($sql);

        $cmd->bindValue(':institutionId', $institutionId);

        if ($filter != '0') {
            $filter = '%' . $filter . '%';
            $cmd->bindValue(':filter', $filter);
        }

        $result = $cmd->queryAll();

        return $result;
    }

    /**
     *获取机构所有联系人列表
     * @param $institutionId
     * @return array
     */
    public static function getInstitutionAllContacterList($institutionCode)
    {
        $sql = "SELECT * FROM t_institution_contacter WHERE institution_code= :institutionCode and isdelete=0 ORDER BY create_datetime DESC";
        $cmd = \Yii::$app->db->createCommand($sql);
        $cmd->bindValue(':institutionCode', $institutionCode);
        $result = $cmd->queryAll();
        return $result;
    }

    /**
     * 获取机构联系人详情
     * @param $userId
     * @return array|false
     */
    public static function getInstitutionContacter($userId)
    {
        $sql = "SELECT * FROM t_institution_contacter WHERE id= :userId";
        $cmd = \Yii::$app->db->createCommand($sql);
        $cmd->bindValue(':userId', $userId);
        $result = $cmd->queryOne();

        return $result;
    }

    /**
     * 管理机构联系人
     * @param $record (code,id,name,phone,email,remark,isDelete)
     * @return false|null|string
     */
    public static function manageInstitutionContacter($record)
    {
        $id = null;
        $transaction = \Yii::$app->db->beginTransaction();
        try {
            $sql = "call p_save_institution_contacter(:in_code,:in_id,:in_name,:in_phone,:in_email,:in_remark,:in_isDelete, @out_id)";

            $cmd = \Yii::$app->db->createCommand($sql);
            $cmd->bindValue(':in_code', $record['code']);
            $cmd->bindValue(':in_id', $record['id']);
            $cmd->bindValue(':in_name', $record['name']);
            $cmd->bindValue(':in_phone', $record['phone']);
            $cmd->bindValue(':in_email', $record['email']);
            $cmd->bindValue(':in_remark', $record['remark']);
            $cmd->bindValue(':in_isDelete', $record['isDelete']);

            $result = $cmd->queryScalar();

            if ($result == 0) {
                $cmd = \Yii::$app->db->createCommand('SELECT @out_id');
                $id = $cmd->queryScalar();
            } else {
                $id = null;
            }

            $transaction->commit();
        } catch (\Exception $e) {
            $transaction->rollBack();
            $id = null;
        }

        return $id;
    }

    /**
     * 前端模板图片
     * @param $type
     * @return array
     */
    public static function getTemplate($type)
    {
        $sql = "SELECT * FROM t_theme_template WHERE theme_type= :type";
        $cmd = \Yii::$app->db->createCommand($sql);
        $cmd->bindValue(':type', $type);
        $result = $cmd->queryAll();

        return $result;
    }

    /**
     * 根据机构编码或者名称查询机构编码（检查机构是否存在）
     * @param $institution_code
     * @param $institution_name
     * @return false|null|string
     */
    public static function getInstCodeByNameOrCode($institution_code, $institution_name)
    {
        //条件都为空时返回null
        if (($institution_code == null || $institution_code == '') && ($institution_name == null || $institution_name == '')) {
            return null;
        }

        $filterSQL = '';
        //添加查询条件
        if ($institution_code != null && $institution_code != '') {
            $filterSQL = $filterSQL . " and t.code = :institution_code ";
        }
        if ($institution_name != null && $institution_name != '') {
            $filterSQL = $filterSQL . " and t.name = :institution_name ";
        }
        //查询SQL
        $sql = "select t.code,t.parent_code,case when s.name is null then '无' else s.name end as parent_name from t_institution t left join t_institution s on s.code = t.parent_code where 1=1 $filterSQL ";

        //创建查询
        $cmd = \Yii::$app->db->createCommand($sql);
        //绑定参数
        if ($institution_code != null && $institution_code != '') {
            $cmd->bindValue(':institution_code', $institution_code);
        }
        if ($institution_name != null && $institution_name != '') {
            $cmd->bindValue(':institution_name', $institution_name);
        }
        //执行获取结果
        $result = $cmd->queryAll();
        return $result;
    }

    /**
     * 根据域名来获取机构信息
     * @param $domain
     * @return array|false|null
     */
    public static function getInstitutionByDomain($domain)
    {
        $cmd = \Yii::$app->db->createCommand('
            SELECT t1.*, t0.site_name, t0.theme_pc, t0.theme_m FROM t_institution_site_config t0
            LEFT JOIN t_institution t1 ON t1.`code`=t0.institution_code
            WHERE t0.domain=:domain AND t1.is_enabled=1');
        $cmd->bindValue(':domain', $domain);

        $item = DbCacheUtility::queryOne($cmd, 10);
        if ($item == false) {
            return null;
        } else {
            return $item;
        }
    }

    /**
     * 根据机构编码来获取机构信息
     * @param $institutionCode
     * @return array|false|null
     */
    public static function getInstitutionByCode($institutionCode)
    {
        $cmd = \Yii::$app->db->createCommand('
            SELECT t1.*, t0.site_name, t0.theme_pc, t0.theme_m FROM t_institution_site_config t0
            LEFT JOIN t_institution t1 ON t1.`code`=t0.institution_code
            WHERE t1.`code`=:institutionCode AND t1.is_enabled=1');
        $cmd->bindValue(':institutionCode', $institutionCode);

        $item = DbCacheUtility::queryOne($cmd, 0);
        if ($item == false) {
            return null;
        } else {
            return $item;
        }
    }

    /**
     * 返回机构下拉列表内容及当前被选中的机构，操作人机构0返回所有机构，否则只返回一个机构
     * @param $institution 操作人员机构
     * @param $selectedInstitution 当前选择的机构
     * @return mixed
     */
    public static function getInstitutionSelectList($institution, $selectedInstitution)
    {
        //如果未选择机构，则返回当前用户登录的机构
        if ($selectedInstitution == '') {
            $selectedInstitution = $institution;
        }

        //返回当前选中的机构
        if ($selectedInstitution == '0') {
            $institutionRecord['code'] = '0';
        } else {
            $institutionRecord = self::getInstitution($selectedInstitution);
        }
        $result['selectedInstitution'] = $institutionRecord['code'];

        //下拉列表内容
        if ($institution == '0') {
            $institutionList = self::getAllEnabledListWithHierarchy($institution);

        } else {

            $institutionList[0]["code"] = $institutionRecord["code"];
            $institutionList[0]["name"] = $institutionRecord["name"];
        }
        $result['institutionList'] = $institutionList;

        return $result;

    }

    /**
     * 获取某机构的所有旗下机构信息,用于dashboard数据展现
     * @param $institutionCode
     * @return array
     */
    public static function getListByDashboard($institutionCode)
    {
        $cmd = \Yii::$app->db->createCommand('
SELECT t0.code, t0.parent_code, t0.name, t0.is_enabled, t0.create_datetime, t0.remark,
ifnull(t1.balance, 0) AS balance, ifnull(t1.credit_quota, 0) AS credit_quota, ifnull(t2.total_settlement_price, 0) AS total_settlement_price FROM t_institution t0
LEFT JOIN t_institution_account t1 ON t1.institution_code=t0.code
LEFT JOIN (
    SELECT
      sum(total_settlement_price) AS total_settlement_price,
      institution_code
    FROM t_order_flow
    GROUP BY institution_code
    ) t2 ON t2.institution_code=t0.code
WHERE t0.parent_code=:institutionCode');
        $cmd->bindValue(':institutionCode', $institutionCode);
        $items = $cmd->queryAll();
        return $items;
    }

    /**
     * 获取子机构
     * @param $parent_code
     * @return array
     */
    public static function getInstitutionByParentCode($parent_code)
    {
        $sql = 'SELECT * FROM t_institution WHERE parent_code = :parent_code AND is_enabled = 1';
        $cmd = \Yii::$app->db->createCommand($sql);
        $cmd->bindValue(':parent_code', $parent_code);

        $result = $cmd->queryAll();
        return $result;
    }

    /**
     * 判断$code是否$parent_code的直属下级机构
     * @param $parent_code
     * @param $code
     * @return bool
     */
    public static function isUnderInstitution($parent_code, $code)
    {
        $sql = 'SELECT count(*) FROM t_institution WHERE parent_code = :parent_code AND code = :code';
        $cmd = \Yii::$app->db->createCommand($sql);
        $cmd->bindValue(':parent_code', $parent_code);
        $cmd->bindValue(':code', $code);
        $result = $cmd->queryScalar();
        if ($result == '0') {
            return false;
        }
        return true;
    }

    public static function getInstitutionAccountListTotal($total, $institution)
    {
        $filterSQL = '1=1 ';
        if ($institution != '0') {
            $filterSQL = $filterSQL . ' and code= :institution';
        }
        if ($total != '') {
            $filterSQL = $filterSQL . ' and total <= :total';
        }
        $sql = "SELECT count(*) AS totalnum FROM v_institution_account where $filterSQL";
        $cmd = \Yii::$app->db->createCommand($sql);
        if ($institution != '0') {
            $cmd->bindValue(':institution', $institution);
        }
        if ($total != '') {
            $cmd->bindValue(':total', $total);
        }

        $result = $cmd->queryOne();
        return $result['totalnum'];
    }

    public static function getInstitutionAccountList($start, $pageSize, $total, $institution, $orderBy)
    {
        $filterSQL = '1=1 ';
        if ($institution != '0') {
            $filterSQL = $filterSQL . ' and code= :institution';
        }
        if ($total != '') {
            $filterSQL = $filterSQL . ' and total <= :total';
        }
        $sql = "SELECT *  FROM v_institution_account where $filterSQL order by total $orderBy ,parent_code LIMIT $start,$pageSize";
        $cmd = \Yii::$app->db->createCommand($sql);
        if ($institution != '0') {
            $cmd->bindValue(':institution', $institution);
        }
        if ($total != '') {
            $cmd->bindValue(':total', $total);
        }

        $result = $cmd->queryAll();
        return $result;
    }

    /**获取机构信息
     * @param $institutionCode
     * @return array|false
     */
    static public function getInstitutionInfo($institutionCode)
    {
        $sql = 'select * from t_institution where code=:institution_code';
        $cmd = \Yii::$app->db->createCommand($sql);
        $cmd->bindValue(':institution_code', $institutionCode);
        return $cmd->queryOne();
    }

    /**
     * 获取特殊定价机构
     * @return mixed
     */
    public static function getCustomPriceInstitutionList()
    {
        $sql = 'select `code` from t_institution where is_custom_price=1';
        $cmd = \Yii::$app->db->createCommand($sql);
        return $cmd->queryColumn();
    }
}